<?php
/**
 * 配送地区管理控制器
 * @version 2015070610 
 * @author Justin <justin@jipu.com>
 */

namespace Admin\Controller;

class AreaController extends AdminController {

  public function index(){
    //记录当前列表页的Cookie
    Cookie('__forward__', $_SERVER['REQUEST_URI']);
    $pid = I('get.pid');
    $title = I('get.title');
    $where['a.pid'] = $pid ? $pid : 0;
    $title && $where['a.title'] = array('like', '%'.$title.'%');
    $order = '`sort` DESC , `id` ASC';
    $field = 'a.id,a.pid,a.title,a.sub_name,a.sort,a.zone_id,z.name';
    $limit = 10;
    
    $prefix = C('DB_PREFIX');
    $area   = $prefix.'area';
    $zone   = $prefix.'area_zone';
    $model  = M()->table( $area.' a' )->join ( ' LEFT JOIN '.$zone.' z ON a.zone_id=z.id' );
    $lists = $this->lists($model,$where,$order,$field,$limit,array());
    $this->assign('lists',$lists);
    $this->display();
  }
  
  function add(){
    //获取上级地区
    I('get.pid') > 0 && $this->data_up = M('Area')->getById(I('get.pid'));
    //读取区域
    $zone = M('AreaZone')->where(array('status'=>1))->limit(100)->select();
    $this->assign('zone',$zone);
    parent::add();
  }
  
  /**
  * 编辑display前置方法
  * @version 2015070617 
  * @author Justin <justin@jipu.com>
  */
  function _before_edit_display(){
    //获取上级地区
    $this->data['pid'] > 0 && $this->data_up = M('Area')->getById($this->data['pid']);
    //读取区域
    $zone = M('AreaZone')->where(array('status'=>1))->limit(100)->select();
    $this->assign('zone',$zone);
  }

  /**
   * 删除一个地区
   * @author Max.Yu <max@jipu.com>
   */
  public function remove(){
    $area_id = I('id');
    if(empty($area_id)){
      $this->error('参数错误!');
    }

    //判断该地区下有没有子地区，有则不允许删除
    $child = M('Area')->where(array('pid'=>$area_id))->field('id')->select();
    if(!empty($child)){
      $this->error('请先删除该地区下的子地区');
    }

    //删除该地区信息
    $res = M('Area')->delete($area_id);
    if($res !== false){
      $this->success('删除地区成功！');
    }else{
      $this->error('删除地区失败！');
    }
  }
  
  
  /**
   * 区域列表
   */
  public function zone(){
        $map['status'] = array('egt',0);
        $name = I('get.name');
        $name && $map['name'] = $name;
        $order = '`sort` DESC , `id` ASC';
        $lists = $this->lists('area_zone', $map,$order);
        int_to_string($lists, array('status' => array('0' => '<span class="text-cancel">已禁用</span>', '1' => '<span class="text-success">正常</span>')));
        $this->lists = $lists;
        $this->meta_title = '地区区域列表';
        $this->display();
  }
  
  
  /**
   * 区域编辑、新增
   */
  public function addZone(){
      $id = I('get.id');
      $model = D('AreaZone');
      if(IS_POST && IS_AJAX){
          $id = I('id');
          $saveData['name'] = I('name');
          $saveData['sort'] = I('sort');
          $saveData['status'] = I('status');
          if(empty($saveData)){
              $this->error('区域名称必须填写');
              return false;
          }
          if($id){
              $rst = $model->where(array('id'=>$id))->save($saveData);
              $type = '编辑';
          }else{
              $rst = $model->add($saveData);
              $type = '新增';
          }
          ($rst !== false) ? $this->success($type.'成功','/Admin/Area/zone') : $this->error($type.'失败');
          exit;
      }
      $data = array();
      if($id){
          $meta_title = '编辑区域';
          $data = $model->where(array('id'=>$id))->find();
      }else{
          $meta_title = '新增区域';
      }
      $this->assign('data',$data);
      $this->assign('meta_title',$meta_title);
      $this->display();
  }
  
  
  /**
   * 删除地区
   */
  public function delZone(){
      $id = I('id');
      if(empty($id)){
          $this->error('请选择要删除的数据');
      }
      if(in_array($id,C('DEFAULT_AREA_ZONE'))){
          $this->error('禁止删除系统数据');
      }
      $map['id'] = $id;
      $rst = M('AreaZone')->where($map)->delete();
      $rst ? $this->success('成功删除') : $this->error('删除失败！');
  }
  

}
